3 use MediaWiki\Shell\Command
;
8 class CommandTest
extends PHPUnit_Framework_TestCase
{
9 private function requirePosix() {
10 if ( wfIsWindows() ) {
11 $this->markTestSkipped( 'This test requires a POSIX environment.' );
16 * @dataProvider provideExecute
18 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
19 $this->requirePosix();
21 $command = new Command();
23 ->params( $commandInput )
26 $this->assertSame( $expectedExitCode, $result->getExitCode() );
27 $this->assertSame( $expectedOutput, $result->getStdout() );
30 public function provideExecute() {
32 'success status' => [ 'true', 0, '' ],
33 'failure status' => [ 'false', 1, '' ],
34 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
38 public function testEnvironment() {
39 $this->requirePosix();
41 $command = new Command();
43 ->params( [ 'printenv', 'FOO' ] )
44 ->environment( [ 'FOO' => 'bar' ] )
46 $this->assertSame( "bar\n", $result->getStdout() );
49 public function testStdout() {
50 $this->requirePosix();
52 $command = new Command();
55 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
58 $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
59 $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
62 public function testStdoutRedirection() {
63 $this->requirePosix();
65 $command = new Command();
68 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
69 ->includeStderr( true )
72 $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
73 $this->assertNull( $result->getStderr() );
76 public function testOutput() {
79 $this->requirePosix();
82 $command = new Command();
84 ->params( [ 'ls', 'index.php' ] )
86 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
87 $this->assertSame( null, $result->getStderr() );
89 $command = new Command();
91 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
94 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
95 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
96 $this->assertSame( null, $result->getStderr() );
98 $command = new Command();
100 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
102 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
103 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
106 public function testT69870() {
107 $commandLine = wfIsWindows()
109 ?
( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
110 : 'printf "%-333333s" "*"';
112 // Test several times because it involves a race condition that may randomly succeed or fail
113 for ( $i = 0; $i < 10; $i++
) {
114 $command = new Command();
115 $output = $command->unsafeParams( $commandLine )
118 $this->assertEquals( 333333, strlen( $output ) );